//
//  RegViewController.m
//  IFengNews
//
//  Created by lanou3g on 16/3/7.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "RegViewController.h"

@interface RegViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userName;

@property (weak, nonatomic) IBOutlet UITextField *passWord;
@property (weak, nonatomic) IBOutlet UITextField *secondPassWord;

@end

@implementation RegViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //点击空白处回收键盘
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(returnKeyBoard)];
    [self.view addGestureRecognizer:tap];
}
- (void)returnKeyBoard{
    [self.view endEditing:YES];
}
//点击注册按钮调用的方法
- (IBAction)regSuccess:(id)sender {
    
    NSString *name = self.userName.text;
    NSString *passWord = self.passWord.text;
    NSString *secondPassWord = self.secondPassWord.text;
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    //如果账户名已经注册
    if ([user objectForKey:name]!= nil) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"账户名已注册" preferredStyle:(UIAlertControllerStyleAlert)];
        [self presentViewController:alert animated:YES completion:^{
            sleep(1);
            [self dismissViewControllerAnimated:YES completion:nil];
            return;
        }];
    }
    
    if([name length]!= 0 && [passWord length]!= 0)
    {//注册的账户名与密码不能为空
        if ([passWord isEqualToString:secondPassWord] )
        {//判断两次密码是否相同
            //KVC方法
            [user setValue:passWord forKey:[NSString stringWithFormat:@"%@",name]];
            

            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:(UIAlertControllerStyleAlert)];
            [self presentViewController:alert animated:YES completion:^{
                sleep(1);
                [self dismissViewControllerAnimated:YES completion:^{
                    [self dismissViewControllerAnimated:YES completion:nil];
                }];
            }];
        }else
        {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"两次输入的密码不同，请重新输入" preferredStyle:(UIAlertControllerStyleAlert)];
            [self presentViewController:alert animated:YES completion:^{
                sleep(1);
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
        }
    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"输入的账号/密码不能为空，请重新输入" preferredStyle:(UIAlertControllerStyleAlert)];
        [self presentViewController:alert animated:YES completion:^{
            sleep(1);
            [self dismissViewControllerAnimated:YES completion:nil];
        }];

    }
         
    
}

- (IBAction)secReg:(id)sender {
    
    //直接返回登陆界面
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
    
}




@end
